package player

import (
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/m"
	"GameServer/Game/sp"
)

// 获取新兵特训状态 8660
func (p *Player) RecruitTrainRequest(req *sp.RecruitTrainRequest) (res *sp.RecruitTrainResponse, err error) {
	us := p.User
	// 玩家没有到达1-2，不获取新兵特训
	if us.MaxPos.LockBlockTid < 2 {
		res = &sp.RecruitTrainResponse{}
		return
	}
	lst, err := lib.GetRecruitTrain(p.User)
	if err != nil {
		res = &sp.RecruitTrainResponse{}
		return
	}
	res = &sp.RecruitTrainResponse{
		Lst: lib.FormatRecruitTrainSps(lst),
	}
	return
}

// 领取新兵特训奖励 8661
func (p *Player) RecruitTrainRewardRequest(req *sp.RecruitTrainRewardRequest) (res *sp.RecruitTrainRewardResponse, err error) {
	resList, err := lib.RecruitTrainReward(p.User.Uid.Hex(), req.Tid)
	if err != nil {
		res = &sp.RecruitTrainRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	uProp, resGoods, boxResList, err := lib.GiveGoodsByKvList("RecruitTrainRewardRequest", p.User, resList, p.Send)
	if err != nil {
		res = &sp.RecruitTrainRewardResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	lst, _ := lib.GetRecruitTrain(p.User)
	fidx := -1
	for idx, item := range lst {
		if item.Tid == req.Tid {
			fidx = idx
		}
	}
	var rst *m.RTState
	if -1 < fidx && fidx < len(lst)-1 {
		rst = lst[fidx+1]
	}

	res = &sp.RecruitTrainRewardResponse{
		Res:    lib.FormatGoodsInfoSps(resGoods),
		BoxRes: lib.FormatBoxResSps(boxResList),
		State:  lib.FormatRTState(rst),
	}
	return
}
